package com.qf.sl.youshare.tasks;

import android.os.AsyncTask;
import com.qf.sl.youshare.Constants;
import com.qf.sl.youshare.utils.HttpTools;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * Created by IDEA
 * User : SL
 * on  2015/3/6.
 * 19:57
 */

/**
 * 该异步任务需要传递三个参数，并且这三个参数必须要按照 userid,goods_id,num
 * 的顺序传递
 */
public class AddToCartTask extends AsyncTask<String, Integer, JSONObject> {

    private TaskProcessor processor;

    public AddToCartTask(TaskProcessor processor) {
        this.processor = processor;
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        JSONObject ret = null;

        String userId = null;
        String goods_id = null;
        String num = null;

        if (params != null && params.length > 2) {

            userId = params[0];
            goods_id = params[1];
            num = params[2];

            String url = Constants.CART_ADD_URL +
                         "&userid=" + userId +
                         "&goods_id=" + goods_id +
                         "&num=" + num;

            byte[] bytes = HttpTools.get(url);
            try {
                String string = new String(bytes,"UTF-8");
                ret = new JSONObject(string);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {
        if (processor != null) {
            processor.processResult(jsonObject);
        }
    }
}
